package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.weather.pojo.CityWeather;
import com.example.weather.utils.TokenUtil;
import com.example.weather.utils.WeatherUtil;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Spinner sp_chooseWeather;
    String[] citys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp_chooseWeather = findViewById(R.id.sp_chooseWeather);
        citys  = new String[]{"北京市", "上海市", "天津市", "大连市"};
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.item_city,citys);
        sp_chooseWeather.setAdapter(arrayAdapter);
        sp_chooseWeather.setSelection(0);
        sp_chooseWeather.setOnItemSelectedListener(this);
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        new Thread(()->{
            try {
                String token = TokenUtil.getToken();
//                System.out.println("token:"+token);
                CityWeather cityWeather = WeatherUtil.getWeatherForCity(citys[position],token);
//                System.out.println("cityWeather:"+cityWeather);

                runOnUiThread(()->{
                    //获取所有的显示位置
                    TextView tv_cityName = findViewById(R.id.tv_cityName);
                    TextView tv_cityTemperature = findViewById(R.id.tv_cityTemperature);
                    TextView tv_cityWeatherConditions = findViewById(R.id.tv_cityWeatherConditions);
                    TextView tv_cityMaxTemperature = findViewById(R.id.tv_cityMaxTemperature);
                    TextView tv_cityMinTemperature = findViewById(R.id.tv_cityMinTemperature);
                    TextView tv_cityHumidity = findViewById(R.id.tv_cityHumidity);
                    TextView tv_cityWindVelocity = findViewById(R.id.tv_cityWindVelocity);

                    //对每个显示位置进行渲染
                    tv_cityName.setText(cityWeather.getArea());
                    tv_cityTemperature.setText(cityWeather.getCurrentTemperature()+"");
                    tv_cityWeatherConditions.setText(cityWeather.getWeatherConditions());
                    tv_cityMaxTemperature.setText(cityWeather.getIntervalTemperature().split("~")[1]+"度");
                    tv_cityMinTemperature.setText(cityWeather.getIntervalTemperature().split("~")[0]+"度");
                    tv_cityHumidity.setText(cityWeather.getHumidity()+"%");
                    tv_cityWindVelocity.setText(cityWeather.getWindVelocity()+"Km/h");

                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}